//
//  SecondTwoViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SecondTwoViewController.h"
#import "SecondTwoCollectionViewCell.h"
#import "SecondContentModel.h"
#import "WebViewViewController.h"
@interface SecondTwoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;//CollectionView
@property (nonatomic, retain) NSMutableArray *dataArr;//collectionView数组
@property (nonatomic, retain) UICollectionReusableView *colheader;//collectionView头部
@property (nonatomic, retain) UIImageView *headerimg;//头图片
@property (nonatomic, retain) UILabel *headertitle;//头部标题
@property (nonatomic, retain) UILabel *headercontent;//头部内容;
@property (nonatomic, assign) CGFloat h;//头部字体高度
@end

@implementation SecondTwoViewController

-(void)dealloc
{
    [_collectionView release];
    [_dataArr release];
    [_colheader release];
    [_headercontent release];
    [_headerimg release];
    [_headertitle release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    //数据
    [self data];
#pragma mark - layout布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 20, [UIScreen mainScreen].bounds.size.width / 2 + 50);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//边界
    //头部高度
    layout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 370);
    //行列
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
#pragma mark - 创建CollectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    //代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //背景颜色
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //添加
    [self.view addSubview:self.collectionView];
    //重用池
    [_collectionView registerClass:[SecondTwoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //头部重用池
     [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView release];
    [layout release];
    
    
}
#pragma mark - collectionView必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecondContentModel *s = _dataArr[indexPath.item];
    SecondTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.bgimg sd_setImageWithURL:[NSURL URLWithString:s.picurl]];//图片
    cell.titlelabel.text = s.title;//标题
    cell.price.text = [@"￥"stringByAppendingString:s.yh_price];//价格
    cell.buy.text = [s.sum stringByAppendingString:@"个人购买"];//购买数量
    return cell;
}
#pragma mark - 数据
- (void)data
{
    NSString *str = [[@"http://m.shougongke.com/index.php?c=Shiji&a=topicInfo&topic_id=" stringByAppendingString:self.strid]stringByAppendingString:@"&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        [self creat];
        NSDictionary *dic = result[@"data"];
        self.headercontent.text = [dic objectForKey:@"topic_des"];
//        self.h = [[self class] heightWihtText:_headercontent.text];
        self.headertitle.text = [dic objectForKey:@"topic_name"];
        [self.headerimg sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"host_pic"]]];
        NSArray *arr = result[@"data"][@"goods"];
        self.dataArr = [SecondContentModel modelHandlerWithArray:arr];
        NSLog(@"%@",arr);
        [_collectionView reloadData];
    }];
}
#pragma mark - collectionView的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    self.colheader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    
    return _colheader;
}
- (void)creat
{
  //头部图片
    self.headerimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width ,150)];
    [self.colheader addSubview:self.headerimg];
    //头部title
    self.headertitle = [[UILabel alloc]initWithFrame:CGRectMake(5, 30, [UIScreen mainScreen].bounds.size.width - 10, 80)];
    self.headertitle.textColor = [UIColor whiteColor];//字体颜色
    self.headertitle.font = [UIFont systemFontOfSize:24];//字体大小
    self.headertitle.font = [UIFont boldSystemFontOfSize:26];//粗体
    self.headertitle.textAlignment = NSTextAlignmentCenter;//居中
    self.headertitle.numberOfLines = 0;//不限制行数
    [self.headerimg addSubview:self.headertitle];
    //头部内容
//    CGFloat h = [[self class]heightWihtText:_headercontent.text];
    self.headercontent = [[UILabel alloc]initWithFrame:CGRectMake(10, 150, [UIScreen mainScreen].bounds.size.width - 20,200)];
    self.headercontent.textColor =[UIColor grayColor];//字体颜色
    self.headercontent.numberOfLines = 0;//不限制行数
    self.headercontent.font = [UIFont systemFontOfSize:14];//字体大小
    [self.colheader addSubview:self.headercontent];
    
    
    [_headertitle release];
    [_headerimg release];
    [_headercontent release];
    
}
#pragma mark - 点击条转WebView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewViewController *web = [[WebViewViewController alloc]init];
    SecondContentModel *s = _dataArr[indexPath.row];
    web.strid = s.num_iid;
    [self.navigationController pushViewController:web animated:YES];
}
//+ (CGFloat)heightWihtText:(NSString *)tex
//{
//    // 1.文本属性
//    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
//    // 2.预计尺寸
//    CGSize s = CGSizeMake(375 - 20, 1000);
//    // 3.计算
//    CGRect r = [tex boundingRectWithSize:s options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    return r.size.height;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
